package com.shark.rebirth;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.shark.lib.rebirth.IRebirthProcess;
import com.shark.lib.rebirth.RebirthEnv;
import com.shark.lib.rebirth.RebirthEnvConfigs;

/**
 * @author: guojr
 * @date: 2020/4/15
 */
public class SharkApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
            onInitDaemon();
    }

    private void onInitDaemon() {
        Log.e("APP", "onInitDaemon: "+ getCurrentProcessName(this));
        RebirthEnvConfigs configs = new RebirthEnvConfigs(
                new RebirthEnvConfigs.Config(getPackageName() + ":resident", Service1.class));
        RebirthEnv.init(this, configs);
    }

    private String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
